package sd.czw.com.mybottomnavigationbar;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by czw
 * desc 描述：
 * on 2017/10/16.
 */
public class MyFragment extends Fragment {

    private View rootview;
    private TabLayout tab;
    private ChildViewPager viewPager;
    private String[] titles = {
            "全部", "待付款", "待收货", "待评价", "退款"
    };
    private List<Fragment> list = new ArrayList<>();
    public static MyFragment newInstance(String titles) {
        Bundle args = new Bundle();
        args.putString("title",titles);
        MyFragment fragment = new MyFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.fragment_m,container,false);

        String title = getArguments().getString("title");
//        textview = (TextView) rootview.findViewById(R.id.tv);
//        textview.setText(title);


        tab = (TabLayout) rootview.findViewById(R.id.tab);
        viewPager = (ChildViewPager) rootview.findViewById(R.id.tab_vp);

        for (int i = 0; i < titles.length; i++){
            tab.addTab(tab.newTab().setText(titles[i]));
            list.add(ChildFragment.newInstance(titles[i]));
        }
        Sfragment sfragment = new Sfragment(getActivity().getSupportFragmentManager(), list);
        viewPager.setAdapter(sfragment);
        tab.setupWithViewPager(viewPager);
        tab.setTabsFromPagerAdapter(sfragment);//给Tabs设置适配器
        return rootview;

    }


    class Sfragment extends FragmentPagerAdapter{

        private List<Fragment> fragments;

        public Sfragment(FragmentManager fm,List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
